﻿using Common;
using Common.EF.Controllers;
using Common.HardwareSDKS.Models;
using Common.HardwareSDKS.RS232Reader;
using Common.Model;
using Common.Models;
using Common.Utility;
using Common.Utility.AJ;
using Common.Utility.AJ.EventAgregators;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AWSV2.ViewModels
{
    public class CarLabelTextViewModel : Screen, IHandle<MainShellViewEvent>
    {
        private string _title;
        public string Title
        {
            get => _title;
            set => SetAndNotify(ref _title, value);
        }

        private AJErrorInfoDialogParams _errorInfo;
        public AJErrorInfoDialogParams ErrorInfo
        {
            get => _errorInfo;
            set => SetAndNotify(ref _errorInfo, value);
        }

        private bool _loading;
        public bool Loading
        {
            get => _loading;
            set => SetAndNotify(ref _loading, value);
        }

        private Car _form;
        public Car Form
        {
            get => _form;
            set => SetAndNotify(ref _form, value);
        }

        private RS232ReaderDevice _reader;
        private IEventAggregator _eventAggregator;

        public CarLabelTextViewModel(Car form, IEventAggregator eventAggregator)
        {
            ErrorInfo = new AJErrorInfoDialogParams();
            _eventAggregator = eventAggregator;
            Title = I18NService.Translate(LangKeys._0_绑定卡号, form.PlateNo);
            _form = new Car();
            AJUtil.CopyPropertyValues(_form, form);
            _eventAggregator.Subscribe(this);
        }

        private bool _canCommit;
        public bool CanCommit
        {
            get
            {
                _canCommit = !Loading;
                return _canCommit;
            }
            set => SetAndNotify(ref _canCommit, value);
        }

        public async void Commit()
        {
            if (string.IsNullOrWhiteSpace(_form.LabelNo))
            {
                ErrorInfo.Show(I18NService.Translate(LangKeys._0_必填, I18NService.Translate(LangKeys.卡号)));
                return;
            }

            Loading = true;
            try
            {
                using var carCtrl = new CarController();
                var result = await carCtrl.Save(AJAutoMapperService.Instance()
                    .Mapper.Map<Car, Common.EF.Tables.Car>(_form));

                RequestClose(true);
            }
            catch (Exception e)
            {
                ErrorInfo.Show(e.Message);
            }
            finally
            {
                Loading = false;
            }
        }

        protected override void OnViewLoaded()
        {
            _reader = new RS232ReaderDevice(SettingsHelper.AWSV2Settings, true);
            _reader.SetEventAggregator(_eventAggregator);
            _reader.Open();

        }

        protected override void OnClose()
        {
            _reader.Close();
        }

        public void Handle(MainShellViewEvent message)
        {
            if (message.Type == MainShellViewEvent.EventType.车牌识别)
            {
                Form.LabelNo = (message.Data as DeviceInfo).CarIdentificationPlateResult.CarNo;
            }
        }
    }
}
